# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

##
## Copyright (C) 2009 - George Y. Kussumoto <georgeyk.dev@gmail.com>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
##
""" Statitical handling """

import re


class TweetPicture(object):
    #TODO: Add more services here.
    urls = ('http://twitpic.com/',
            'http://img.ly/',
            'http://yfrog.com',
            'http://www.tweetphoto.com',
            'http://pikchur.com/',
            'http://twitgoo.com',
            'http://www.picktor.com/',)


    @classmethod
    def is_picture(self, tweet):
        for url in self.urls:
            if url in tweet:
                return True
        return False


def classify_tweets(tweets):
    classification = dict(tweets=0,
                          rts=0,
                          pics=0,
                          links=0,
                          answers=0,
                          trends=0)
    for tweet in tweets:
        msg = tweet.GetText()
        #classification['tweets'] += 1
        if 'RT ' in msg:
            classification['rts'] += 1
        elif TweetPicture.is_picture(msg):
            classification['pics'] += 1
        elif 'http://' in msg:
            classification['links'] += 1
        elif re.match('@.*', msg) is not None:
            classification['answers'] += 1
        elif re.match('#.*', msg) is not None:
            classification['trends'] += 1

    # FIXME:
    # A little hack to get the winner attribute by sorting the dictionary by
    # the values - not really well tested.
    return sorted(classification.items(), key=lambda (k, v): v)[-1]
